﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.HomeServer.Common.Client;
using RouterControl.LocalDb;

namespace RouterControl.WhsAddIn.SettingsControls
{
    public partial class LocalDatabase : SettingsControlBase
    {
        private LocalDbConnector _conn;

        public LocalDatabase(RouterControlSettingsTab2 tab)
			: base(tab)
		{
			if (_log.IsDebugEnabled)
				_log.Debug("Initialization start");

			try
			{
				InitializeComponent();

                this.lineBox.Header.Text = Properties.Resources.txtSettingsLocalDb;
                this.statusLabel.Text = Properties.Resources.txtLoading;
	
				// done
				if (_log.IsDebugEnabled)
					_log.Debug("Initialization done");
			}
			catch (Exception e)
			{
				_log.Fatal("Failed to initialize", e);

				throw;
			}
		}

		private void UpdateStatus()
		{
// 			if (_log.IsDebugEnabled)
// 				_log.Debug("Updating status...");

			try
			{
                var size = _conn.GetDatabaseFileSize();
                var unusedPercent = _conn.GetUnusedPagesPercentage();

                this.statusLabel.Text = string.Format(
                    Properties.Resources.txtSettingsLocalDbStatus,
                    FormatHelpers.GetNiceFileSize(size),
                    size,
                    unusedPercent
                    );

                // warning status
                if (unusedPercent > Properties.Settings.Default.LocalDbUnusedPercentWarn)
                {
                    this.statusImage.Image = CommonImages.StatusAtRisk24;
                }
                else
                {
                    this.statusImage.Image = CommonImages.StatusHealthy24;
                }
			}
			catch (Exception ex)
			{
				_log.Error("Failed to update status", ex);
			}
		}

        private void maintainLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                using (new UserRecognizeWait(true, Properties.Resources.txtMaintainingLocalDb))
                {
                    _conn.MaintainDatabase();

                    UpdateStatus();
                }
            }
            catch (Exception ex)
            {
                _log.Error("Failed to update Local DB status manually", ex);

                Microsoft.HomeServer.Controls.QMessageBox.Show(
                    ex.Message,
                    Properties.Resources.txtManualLocalDbMaintainceFailed,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
            }
        }

        private void LocalDatabase_Load(object sender, EventArgs e)
        {
			if (_log.IsDebugEnabled)
				_log.Debug("Loading start");

			try
			{
                _conn = new LocalDbConnector();

                UpdateStatus();

                this.updateStatusTimer.Enabled = true;

                // done
                if (_log.IsDebugEnabled)
                    _log.Debug("Loading done");
            }
            catch (Exception ex)
            {
                _log.Fatal("Loading failed", ex);

                throw;
            }
        }

        private void updateStatusTimer_Tick(object sender, EventArgs e)
        {
            UpdateStatus();
        }
    }
}
